package com.example.myapplication22;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.myapplication22.utils.MyHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class List extends AppCompatActivity {
    ListView lv;
   Button add;
    TextView text1;
    ImageView lay_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        add=findViewById(R.id.add);
        text1=findViewById(R.id.text1);
        lay_img=findViewById(R.id.lay_img);
        //


        SQLiteDatabase w = new MyHelper(List.this).getWritableDatabase();
        Bundle bu = getIntent().getExtras();
        final String id = bu.getString("id");
        //查询出用户头像
        SQLiteDatabase write = new MyHelper(List.this).getWritableDatabase();
        Cursor cursor1 = write.rawQuery("select * from user where userId=?", new String[]{id});
        cursor1.moveToNext();
        int img = cursor1.getInt(3);
        lay_img.setImageResource(img);
        //


        Cursor cursor = w.rawQuery("select * from info where userId=?", new String[]{id});
        text1.setText("当前用户:"+id);
        final   String[] n=new String[cursor.getCount()];//_id
        int j=0;

        //list参数
        java.util.List<Map<String,String>> list=new ArrayList<>();
        Map<String,String > map;
        if (cursor.getCount()!=0){
            while (cursor.moveToNext()){
                n[j]=cursor.getString(0);
                j++;
                 map=new HashMap<>();
                map.put("title","标题:"+cursor.getString(2));
                String s="";
                if (cursor.getString(3).length()>3){
                    s=cursor.getString(3).substring(0,3)+"...";
                }else{
                    s=cursor.getString(3);
                }
                map.put("content","内容:"+s);
                map.put("date",cursor.getString(5));
                list.add(map);

            }
        }
        w.close();

        lv = findViewById(R.id.listView);
        String[] s=new String[]{"title","content","date"};
        int[] i=new int[]{R.id.title,R.id.content,R.id.date};
        final SimpleAdapter simpleAdapter = new SimpleAdapter(List.this,list,R.layout.activity_list_item,s,i);
        lv.setAdapter(simpleAdapter);

        //单击lisView 事件 修改内容
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //i单击的位置
                String _id=n[i];//_id
                //跳转到修改页面
                Intent intent = new Intent(List.this, Update.class);
                Bundle b = new Bundle();
                b.putString("_id",_id);
                b.putString("id",id);//userId
                intent.putExtras(b);
                startActivity(intent);


            }
        });

        //长按listView 事件 删除内容
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            SQLiteDatabase write = new MyHelper(List.this).getWritableDatabase();
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                //i单击的位置
                String _id=n[i];//_id
                System.out.println("长按触发成功");
                SQLiteDatabase w = new MyHelper(List.this).getWritableDatabase();
                int delete = write.delete("info","_id=?",new String[]{_id});
                //弹出框
                AlertDialog dialog = null;
                Cursor cursor = w.rawQuery("select * from info where userId=?", new String[]{id});
                text1.setText("当前用户:"+id);
                final   String[] n=new String[cursor.getCount()];//_id
                int j=0;

                //list参数
                java.util.List<Map<String,String>> list=new ArrayList<>();
                Map<String,String > map;
                if (cursor.getCount()!=0){
                    while (cursor.moveToNext()){
                        n[j]=cursor.getString(0);
                        j++;
                        map=new HashMap<>();
                        map.put("title","标题:"+cursor.getString(2));
                        String s="";
                        if (cursor.getString(3).length()>3){
                            s=cursor.getString(3).substring(0,3)+"...";
                        }else{
                            s=cursor.getString(3);
                        }
                        map.put("content","内容:"+s);
                        map.put("date",cursor.getString(5));
                        list.add(map);

                    }
                }
                w.close();

                lv = findViewById(R.id.listView);
                String[] s=new String[]{"title","content","date"};
                int[] q=new int[]{R.id.title,R.id.content,R.id.date};
                final SimpleAdapter simpleAdapter = new SimpleAdapter(List.this,list,R.layout.activity_list_item,s,q);
                lv.setAdapter(simpleAdapter);

                return delete>0;

            }

        });







        //添加按钮事件
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(List.this, Context.class);
                Bundle bundle = new Bundle();
                bundle.putString("id",id);//userId;

                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}
